/**
 * 用户repairCard.js
 * 版本号： v1.0.0
 */
$(function(){
    var projectId = $('#prId_').val();
    var lastChecked = null;
    $("#repairCard_dataList").datagrid({
        fitColumns: true,
        fit:true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        striped: true,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/repairCard/repairCardList.do?projectId=' + projectId,
        idField: 'repairCardId',
        frozenColumns: [[
            {field: 'repairCardId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'projectName', title: '所属项目', width: 180, fixed: true},
                {field: 'userName', title: '人员姓名', width: 80, fixed: true},
                {field: 'idCard', title: '身份证号', width: 150, fixed: true},
                {field: 'sex', title: '性别', width: 50, fixed: true},
                {field: 'groupName', title: '班组', width: 100, fixed: true},
                {
                    field: 'cardRepairDate', title: '补卡日期', width: 100, fixed: true,
                    formatter: function (value) {
                        if (value != '') {
                            return formatDatebox(value, "yyyy-MM-dd", true)
                        }
                    }
                },
                {field: 'repairPerson', title: '补卡人', width: 100, fixed: true},
                {field: 'entryDate', title: '录入时间', width: 100}
            ]
        ],
        onCheck: function (rowIndex, rowData) {
            var row = $("#repairCard_dataList").datagrid("getChecked");
            if (row && rowIndex == lastChecked) {
                $('#repairCard_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            } else {
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#repairCard_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#repairCard_menu").dialog({
        title: '',
        width: 'auto',
        height: 35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //查询按钮
                if ("searchBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            repairCard_findList();
                        }
                    });
                }
                //添加按钮
                if ("addBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            repairCard_add();
                        }
                    });
                }
                //修改按钮
                if ("editBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            var checked = $("#repairCard_dataList").datagrid("getChecked");
                            if (!checked || checked.length == 0) {
                                $.messager.show({
                                    title: '操作提示',
                                    msg: "请选择一条数据后再进行修改操作",
                                    timeout: 3000,
                                    showType: "slide"
                                });
                                return;
                            }
                            ;
                            var row = $("#repairCard_dataList").datagrid("getSelected");
                            var projectId = $('#prId_').val();
                            window.location.href = encodeURI(CONTEXT_PATH + "/repairCard/toRepairCardOption.do?id=" + row.repairCardId + "&projectName=" + row.projectName + "&prId=" + projectId);
                        }
                    });
                }
                //详情按钮
                if ("viewBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            repairCard_view();
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 添加补卡信息
 */
function repairCard_add() {
    var projectId = $('#prId_').val();
    window.location.href = CONTEXT_PATH + "/repairCard/toRepairCardOption.do?prId=" + projectId;
}

/**
 * 查看补卡详情
 * @param row
 */
function repairCard_view() {
    var checked = $("#repairCard_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({title: '操作提示', msg: "请选择一条数据后再进行修改操作", timeout: 3000, showType: "slide"});
        return;
    }
    ;
    var row = $('#repairCard_dataList').datagrid('getSelected');
    //IFRAM打开窗口
    window.location.href = encodeURI(CONTEXT_PATH + "/repairCard/repairCardDesc.do?id="+ row.repairCardId+
        "&projectName="+row.projectName);
}

/**
 * 查询补卡列表
 */
function repairCard_findList() {
    //刷新dg页码
    refreshPageNumber("repairCard_dataList");
    $("#repairCard_dataList").datagrid("reload", {userName: $("#userName_").textbox("getValue")});
    //清楚选中项
    $("#repairCard_dataList").datagrid("clearSelections");
}
